<template>
  <el-dialog :title="showTitle" :visible="dialogVisible" @close="handleClose">
    <el-form
      ref="roleDialogForm"
      label-width="120px"
      :model="roleForm"
      :rules="rules"
    >
      <el-form-item prop="name" label="点位名称">
        <el-input
          v-model="roleForm.name"
          placeholder="请输入"
          maxlength="15"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="所在区域" prop="regionId">
        <el-select v-model="roleForm.regionId" style="width:100%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属商圈" prop="businessId">
        <el-select v-model="roleForm.businessId" style="width:100%" placeholder="请选择">
          <el-option v-for="item in liat" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="归属合作商" prop="ownerId">
        <el-select v-model="roleForm.ownerId" style="width:100%" placeholder="请选择">
          <el-option v-for="item in libt" :key="item.id" :label="item.name" :value="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="详情地址" prop="addr">
        <el-select v-model="roleForm.value" placeholder="请选择">
          <el-option
            key="group.label"
            label="北京市"
            value="北京市"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="addr">
        <el-input
          v-model="roleForm.addr"
          type="textarea"
          row="5"
          placeholder="请输入详情地址"
          maxlength="60"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button v-loading="loading" type="primary" @click="handleSumit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getPageApi, BusinessDistrictApi, getBusinessDistrictApi, addPontionApi, updataPontionApi } from '@/api/point'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        regionId: '',
        businessId: '',
        ownerId: '',
        addr: '',
        value: ''
      },
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        regionId: [{ required: true, message: '请输入', trigger: 'blur' }],
        businessId: [{ required: true, message: '请输入', trigger: 'blur' }],
        ownerId: [{ required: true, message: '请输入', trigger: 'blur' }],
        addr: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      loading: false,
      list: [],
      liat: [],
      libt: []
    }
  },
  computed: {
    showTitle() {
      return this.roleForm.id ? '修改点位' : '新增点位'
    }
  },
  created() {
    this.getPage()
    this.BusinessDistrict()
    this.getBusinessDistrict()
  },
  methods: {
    async getPage() {
      const res = await getPageApi()
      // console.log(res)
      this.list = res.data.currentPageRecords
      // console.log(this.list)
    },
    async BusinessDistrict() {
      const ras = await BusinessDistrictApi()
      this.liat = ras.data
    },
    async getBusinessDistrict() {
      const rbs = await getBusinessDistrictApi()
      this.libt = rbs.data.currentPageRecords
    },
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.roleDialogForm.resetFields()
      this.roleForm = {}
    },
    async handleSumit() {
      // console.log('确定')
      try {
        this.$refs.roleDialogForm.validate()
        this.roleForm.id ? await updataPontionApi(this.roleForm) : await addPontionApi(this.roleForm)
        this.handleClose()
        this.$message.success(`${this.roleForm.id ? '修改' : '新增'}成功`)
        this.$parent.position()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style>

</style>
